<?php
/**
 * Created by PhpStorm.
 * User: 段涛
 * Date: 2016/4/5
 * Time: 14:50
 * 2016小影院点播财务统计
 */
namespace Admin\Controller;
class BillingVideoPlayController extends AdminController
{

    public function _initialize ()
    {
        parent::_initialize();
        //本地开发的时候显示TRACE
        $env = getenv('RUNTIME_ENVIROMENT');
        if($env==='DEV'){
            C('SHOW_PAGE_TRACE',true);
        }
    }

    /**
     *直属影院列表
     * 只显示选择了2016计费方式的直属影院
     * FunctionName：cinemaList
     */
    public function cinemaList ()
    {
        $Logic = new \Common\Logic\BillingVideoPlayLogic();
        $get = I('get.', '', 'trim');
        $result = $Logic->getCinemaList($get);

        //输出直属影院计费方式
        $videoPlayType=C('CINEMA_BILLING_VIDEO_PLAY_TYPE');
        $videoPlayType=[''=>'-']+$videoPlayType;

        $this->assign('BillingVideoPlayType', $videoPlayType);
        $this->assign('result', $result);
        $this->assign('page_title', '2016影院计费');
        $this->display();
    }

    /**
     *获取代理商列表
     * 只显示选择了2016计费方式的代理商
     * FunctionName：agentList
     */
    public function agentList ()
    {
        $Logic = new \Common\Logic\BillingVideoPlayLogic();
        $get = I('get.', '', 'trim');
        $this->assign('get',$get);
        $result = $Logic->getAgentList($get);

        // 输出代理商2016计费方式
        $videoPlayType=C('AGENT_BILLING_VIDEO_PLAY_TYPE');
        $videoPlayType=[''=>'-']+$videoPlayType;

        $this->assign('videoPlayType',$videoPlayType);
        $this->assign('result', $result);
        $this->assign('page_title', '2016代理计费');
        $this->display();
    }

    /**
     *影院结算列表
     * FunctionName：cinemaBalance
     * author: DuanTao
     */
    public function cinemaBalance ($fid)
    {
        if ($fid == ''){
            $this->error('非法操作！');
        }
        $logic = new \Common\Logic\BillingVideoPlayLogic();
        $result = $logic->getCinemaBalance($fid);  //获取直属影院内容
        $this->assign('result', $result);
        $find = M('cinema_host')->field('cinema_name,type')->where(array ('id' => $fid))->find();  //获取影院名称
        switch ($find['type']){
            case 4;
                $cinema_name='小帅';
                break;
            case 8:
                $cinema_name='携旅';
                break;
            case 9:
                $cinema_name='耀莱';
                break;
            case 10;
                $cinema_name='迈智威';
                break;
            case 11;
                $cinema_name='1905';
                break;
            default:
                $cinema_name=$find['cinema_name'];
                if(empty($cinema_name)){
                    $cinema_name='--';
                }
                break;
        }
        $this->assign('page_title', $cinema_name . '>> 结算记录');
        $this->display();
    }

    /**
     *代理商结算列表
     * FunctionName：agentBalance
     * author: DuanTao
     */
    public function agentBalance ($id)
    {
        $logic = new \Common\Logic\BillingVideoPlayLogic();
        $result = $logic->getAgentBalance($id); //获取代理商结算表
        $this->assign('result', $result);
        $page_title = M('agent')->where(array ('id' => $id))->getField('agent_name'); //获取代理商名称
        $this->assign('page_title', $page_title . '>>结算记录');
        $this->display();
    }

    /**
     *直属影院审核操作
     * FunctionName：BalanceAudit
     * author: DuanTao
     */
    public function cinemaAudit ()
    {
        if (IS_AJAX){ //审核数据内容保存
            $post = I('post.', '', 'strip_tags,trim');
            $logic = new  \Common\Logic\BillingVideoPlayLogic();
            $result = $logic->SaveCinemaAudit($post['data']);
            $this->ajaxReturn($result, 'json');
        } else{   //输出需要审核的数据内容
            $id = I('get.id');
            $inf = M('billing')->where(array ('id' => $id,'status'=>1))->find();
            if (empty($inf)){  //效验该审核内容是否可以展示
                $this->error('非法操作！');
            }
            $this->assign('inf', $inf);
            $this->assign('page_title', $inf['cinema_name']);
            $this->assign('payForType', C('PAY_FOR_TYPE'));  //获取结算方式
            $user_info = I('session.mt_admin');
            $this->assign('user_name', $user_info['user_info']['user_name']);
            $this->display();
        }
    }

    /**
     *代理商审核操作
     * FunctionName：agentAudit
     */
    public function agentAudit ()
    {
        if (IS_AJAX){ //内容数据保存操作
            $post = I('post.', '', 'strip_tags,trim');
            $logic = new  \Common\Logic\BillingVideoPlayLogic();
            $result = $logic->SaveAgentAudit($post['data']);
            $this->ajaxReturn($result, 'json');
        } else{ //展示审核内容
            $id = I('get.id');
            $inf = M('billing')->where(array ('id' => $id,'status'=>1))->find();
            if (empty($inf)){ //效验审核内容是否可以展示
                $this->error('非法操作！');
            }
            $this->assign('inf', $inf);
            //获取代理商名称
            $agentName = M('agent')->where(array ('id' => $inf['agent_id']))->getField('agent_name'); //获取代理商的名称
            $this->assign('agentName', $agentName);
            $this->assign('payForType', C('PAY_FOR_TYPE'));   //获取结算方式
            $user_info = I('session.mt_admin');
            $this->assign('user_name', $user_info['user_info']['user_name']);
            $this->display();
        }
    }

    /**
     *未结算明细
     * FunctionName：noBalance
     * author: DuanTao
     */
    public function noBalance ()
    {
        $logic = new \Common\Logic\BillingVideoPlayLogic();
        $result = $logic->showNoBalance();
        if ($result['code'] === 0){
            $this->error('对不起，系统正在处理，请稍后查看...');
        }
        if ($result['code'] === 1){
            $this->error('错误条件，请先设置对应2016结算方式...');
        }
        $this->assign('inf', $result);
        if($result['agent_id']>0){
            $this->display('noBalanceAgent');
        }else{
            $this->display('noBalanceCinema');
        }

    }

    /**
     *直营平台结算详情
     * FunctionName：details
     * author: DuanTao
     */
    public function cinemaDetails ($id)
    {
        $inf = M('billing')->where(array ('id' => $id, 'status' => array ('IN', array (1, 2))))->find();
        if (empty($inf)){  //效验审核的内容是否可以展示
            $this->error('非法操作！');
        }
        $logic = new \Common\Logic\BillingVideoPlayLogic();
        $name=$logic->selectCinemaName($inf['fid']);  //获取直属影院名称
        $inf['cinema_name']=$name;
        $this->assign('page_title', $name);
        $result = $logic->getVideoPlayList($inf); //获取消费记录
        $billingType = C('CINEMA_BILLING_VIDEO_PLAY_TYPE');
        $inf['billing_video_play_type_word'] = $billingType[$inf['billing_video_play_type']];    //获取结算方式
        $inf['video_play_price_total']=$result['totalPrice'];
        $this->assign('result', $result);
        $inf['verifier'] = $logic->getCheckBillingName($inf['verifier']);    //审核人
        $inf['status'] = $logic->getBillingStatus($inf['status']); //结算状态
        $inf['formula']=$logic->makeFormulaPrice($inf);  //生成数据结算内容公式
        $inf['end_time']=$inf['end_time']-1; //为了展示显示



        $this->assign('inf', $inf);
        $this->display();
    }

    /**
     *代理商结算详情
     * @param $id代理商ID
    FunctionName：agentDetails
     */
    public function agentDetails ($id)
    {
        $inf = M('billing')->field('*')->where(array ('id' => $id))->find();
        if (empty($inf)){ //效验内容是否可以显示
            $this->error('非法操作！');
        }
        $logic = new \Common\Logic\BillingVideoPlayLogic();
        $result = $logic->getVideoPlayList($inf);  //获取播放记录
        $inf['status'] = $logic->getBillingStatus($inf['status']); //获取审核状态
        $inf['agent_name'] = M('agent')->where(array ('id' => $inf['agent_id']))->getField('agent_name');  //获取代理商名称
        $inf['verifier'] = $logic->getCheckBillingName($inf['verifier']);  //获取审核人名称
        $inf['video_play_price_total']=$result['totalPrice'];
        $inf['cinemaType']= $result['cinemaType']; //影院名称列表
        $agentBillingType = C('AGENT_BILLING_VIDEO_PLAY_TYPE');
        $inf['billingType'] = $agentBillingType[$inf['billing_video_play_type']]; //获取结算方式
        $inf['formula']=$logic->makeFormulaPrice($inf); //获取结算内容计算公式
        $inf['end_time']=$inf['end_time']-1; //为了展示显示
        $this->assign('inf', $inf);
        $this->assign('list', $result['list']);
        $this->assign('page', $result['page']);
        $this->display();
    }

    /**
     *代理商返点结算
     * FunctionName：showAgentYearBilling
     */
    public function showAgentYearBilling ()
    {
        if (IS_AJAX){ //返点结算数据保存
            if (!empty(I('post.id'))){
                $logic = new \Common\Logic\BillingVideoPlayLogic();
                $result = $logic->SaveAgentAudit(array ('id' => I('post.id')), true);
                $this->ajaxReturn($result, 'json');
            }
        } else{  //显示返点数据
            //年选项
            $logic = new \Common\Logic\BillingVideoPlayLogic();
            $result = $logic->billingYearCheck();  //代理商返点数据
            $inf['list'] = $result['list'];
            $inf['page'] = $result['page'];
            $inf['yearList'] = $logic->haveBillingYearType(); //获取有返点数据的年份
            $this->assign('inf', $inf);
            $this->assign('page_title', '返点结算');
            $this->display();
        }
    }
}